package gui;


import javax.swing.*;

import code.KeyStoreManager;

import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.awt.image.*;

/**
 * Background image demo
 */

public class CACreateCertificate extends JPanel {

	Color c= new Color(219, 191, 144);

	Image bgImage=null;
	
	JLabel jLabel = new JLabel("Full Name :");
	JLabel jLabel1 = new JLabel("Password :");
	JLabel jLabel2 = new JLabel("Organizational Unit :");
	JLabel jLabel3 = new JLabel("Organization :");
	JLabel jLabel4 = new JLabel("City :");
	JLabel jLabel5 = new JLabel("State or Provience :");
	JLabel jLabel6 = new JLabel("Country :");
	JLabel jLabel7 = new JLabel("Alias :");
	JLabel jLabel8 = new JLabel("Key Size :");
	JLabel jLabel9 = new JLabel("Not Before (Date) :");
	JLabel jLabel10 = new JLabel("Not After (Date) :");
	JLabel jLabel11 = new JLabel("Create Certificate");

	JTextField jTextField = new JTextField();
	JPasswordField jTextField1 = new JPasswordField();
	JTextField jTextField2 = new JTextField();
	JTextField jTextField3 = new JTextField();
	JTextField jTextField4 = new JTextField();
	JTextField jTextField5 = new JTextField();
	JTextField jTextField6 = new JTextField();
	JTextField jTextField7 = new JTextField();
	JTextField jTextField8 = new JTextField();
	JTextField jTextField9 = new JTextField();
	JTextField jTextField10 = new JTextField();

	JTextField field = new JTextField(20);
	String message="";  //  @jve:decl-index=0:
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	
	public CACreateCertificate(ATOCOmain atocoframe) {
		super(null);
		initialize();
		this.atocoframe=atocoframe;
		
	}
	private void initialize(){
		try {
			getFileImage("images/en/parchment.jpg");
			jLabel.setBounds(new Rectangle(90, 75, 107, 16));
			jLabel1.setBounds(new Rectangle(90, 105, 107, 16));
			jLabel2.setBounds(new Rectangle(90, 135, 119, 16));
			jLabel3.setBounds(new Rectangle(90, 165, 107, 16));
			jLabel4.setBounds(new Rectangle(90, 195, 107, 16));
			jLabel5.setBounds(new Rectangle(90, 225, 113, 16));
			jLabel6.setBounds(new Rectangle(90, 255, 107, 16));
			jLabel7.setBounds(new Rectangle(90, 285, 107, 16));
			jLabel8.setBounds(new Rectangle(90, 315, 107, 16));
			jLabel9.setBounds(new Rectangle(90, 345, 109, 16));
			jLabel10.setBounds(new Rectangle(90, 375, 107, 16));
			jLabel11.setBounds(new Rectangle(150, 405, 173, 37));
			jLabel11.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
			jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					createCert();
				}
			});

			jTextField.setBounds(new Rectangle(210, 75, 115, 20));
			jTextField1.setBounds(new Rectangle(210, 105, 115, 20));
			jTextField2.setBounds(new Rectangle(210, 135, 115, 20));
			jTextField3.setBounds(new Rectangle(210, 165, 115, 20));
			jTextField4.setBounds(new Rectangle(210, 195, 115, 20));
			jTextField5.setBounds(new Rectangle(210, 225, 115, 20));
			jTextField6.setBounds(new Rectangle(210, 255, 115, 20));
			jTextField7.setBounds(new Rectangle(210, 285, 115, 20));
			jTextField8.setBounds(new Rectangle(210, 315, 115, 20));
			jTextField9.setBounds(new Rectangle(210, 345, 115, 20));
			jTextField10.setBounds(new Rectangle(210, 375, 115, 20));
			
			jTextField.setOpaque(false);
			jTextField1.setOpaque(false);
			jTextField2.setOpaque(false);
			jTextField3.setOpaque(false);
			jTextField4.setOpaque(false);
			jTextField5.setOpaque(false);
			jTextField6.setOpaque(false);
			jTextField7.setOpaque(false);
			jTextField8.setOpaque(false);
			jTextField9.setOpaque(false);
			jTextField10.setOpaque(false);

			
			jTextField.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField1.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField2.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField3.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField4.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField5.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField6.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField7.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField8.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField9.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
			jTextField10.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));

			add(jLabel);
			add(jLabel1);
			add(jLabel2);
			add(jLabel3);
			add(jLabel4);
			add(jLabel5);
			add(jLabel6);
			add(jLabel7);
			add(jLabel8);
			add(jLabel9);
			add(jLabel10);
			add(jLabel11);
			add(jTextField);
			add(jTextField1);
			add(jTextField2);
			add(jTextField3);
			add(jTextField4);
			add(jTextField5);
			add(jTextField6);
			add(jTextField7);
			add(jTextField8);
			add(jTextField9);
			add(jTextField10);
			
			
		}
		catch (Exception ex) {
			message="File load failed: "+ex.getMessage();
		}
	}
	public void paintComponent(Graphics g) {
		if (bgImage!=null) {
			g.drawImage(bgImage,0,0,this);
		} else {
			g.drawString(message,40,40);
		}
	}
	private void getFileImage(String filePath) throws InterruptedException, IOException {
		FileInputStream in = new FileInputStream(filePath);
		byte [] b=new byte[in.available()];
		in.read(b);
		in.close();
		bgImage=Toolkit.getDefaultToolkit().createImage(b);
		MediaTracker mt=new MediaTracker(this);
		mt.addImage(bgImage,0);
		mt.waitForAll();
	}
	
	private void createCert() {
		String issuerDN = "CN=" + jTextField.getText() + ", " +
						"OU=" + jTextField2.getText() + ", " +
						"O=" + jTextField3.getText() + ", " +
						"L=" + jTextField4.getText() + ", " +
						"ST=" + jTextField5.getText() + ", " +
						"C=" + jTextField6.getText();
		String subjectDN = issuerDN;
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date notBefore = Calendar.getInstance().getTime();
		Date notAfter = new Date(Calendar.getInstance().getTime().getYear()+1,
				Calendar.getInstance().getTime().getMonth(),
				Calendar.getInstance().getTime().getDay());
		try {
			notBefore = df.parse(jTextField9.getText());
			notAfter = df.parse(jTextField10.getText());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		KeyPair kp = ksm.createKeyPair("RSA", Integer.parseInt(jTextField8.getText()));
//		ksm.saveKeyPair("key-" + jTextField7.getText(), kp, jTextField1.getPassword().toString());
		String passwd = jTextField1.getText();
		PrivateKey priv = (PrivateKey)ksm.getPrivateKey("key-cacert", passwd);
		System.err.println(priv.toString());
		PublicKey pub = (PublicKey)ksm.getPublicKey("key-cacert");
		X509Certificate cert = ksm.generateCertificate(pub, priv, 
	    		BigInteger.ONE, issuerDN, notBefore, notAfter, subjectDN, "MD5WithRSAEncryption");
//		ksm.saveCertificate("cert-" + jTextField7.getText(), cert);
//		ksm.saveKeyStore();
	}
	
	/*public static void main(String[] args) {
		PersonalCreateCertificate bgd=new PersonalCreateCertificate();
		JFrame f=new JFrame("Background image");
		f.setBounds(10,10,415,545);
		f.getContentPane().setLayout(new GridLayout());
		f.getContentPane().add(bgd);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}*/
}
